<template>
  <div class="cart">
    <van-nav-bar
      title="购物车"
      right-text="编辑"
      @click-right="onClickRight"
    />
    <div class="contact">
      <div class="title">
        <div class="fl">总共{{goods.length}}件商品</div>
        <div class="red fr">优惠券</div>
      </div>
      <div class="goodlist" ref="wrapper">
        <div>
          <div v-for="(item,index) in goods" class="good">
            <div class="click">
              <van-checkbox v-model="item.select"></van-checkbox>
            </div>
            <div class="info">
              <div class="img">
                <img :src="item.img"/>
              </div>
              <div class="details">
                <div class="name">{{item.name}}</div>
                <div class="subname fs12">{{item.sub}}</div>
                <div class="prize">
                  <prize v-bind:prize="item.prize"></prize>
                  <span>{{item.unit}}</span>
                </div>
                <div class="ctrl">
                  <div class="add plus" @click="addgood1(index)">
                    +
                  </div>
                  <input maxlength="2" v-model="item.count" class="number"/>
                  <div class="add reduce" @click="reducegood1(index)">
                    -
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

    </div>
    <van-submit-bar v-show="!edit"
                    :price="totle_prize"
                    button-text="提交订单"
                    :disabled="goods.length==0"
                    @submit="onSubmit"
    >
      <van-checkbox v-model="allchecked">全选</van-checkbox>
      <span slot="tip">
    您的收货地址不支持同城送, <span>修改地址</span>
  </span>
    </van-submit-bar>

    <van-submit-bar class="delete" v-show="edit"
                    button-text="删除"
                    :disabled="goods.length==0"
                    @submit="deleteGood"
                    button-type="primary"
    >
      <van-checkbox v-model="allchecked">全选</van-checkbox>
    </van-submit-bar>


  </div>
</template>

<script>
  import Bscroll from 'better-scroll'
  import {Toast, SubmitBar} from 'vant';
  import prize from '../goodinfo/plugs/prize'
  import store from '../../vuex/store'

  export default {
    name: "cart",
    components: {
      prize,
    },
    methods: {
      onClickRight: function () {
        if (this.goods.length > 0) {
          if (this.edit == false) {
            this.edit = true
          } else {
            this.edit = false
          }
        } else {
          Toast('您的购物车是空的哦');
        }
      },
      onSubmit: function () {
        store.commit('sumbitOrder')
        this.$router.push('/order')
      },
      deleteGood:function(){
        var x=this.goods.length;
        var deleteIndex=[]
        for (var i=0;i<x;i++){
          if(this.goods[i].select==true){
            deleteIndex.push(i)
          }
        }
        for(var j=0;j<deleteIndex.length;j++){
          this.goods.splice(deleteIndex[j])
        }
      },
      addgood1: function (v) {
        if (this.goods[v].count < this.goods[v].stock) {
          this.goods[v].count++;
          console.log(store.state.cart)
          this.totle_prize = this.totleprize()
        } else {
          Toast('库存不足');
        }

      },
      reducegood1: function (v) {
        if (this.goods[v].count > 1) {
          this.goods[v].count--
          this.totle_prize = this.totleprize()
        }
      },
      totleprize() {
        var prize = 0
        var x = this.goods.length
        for (var i = 0; i < x; i++) {
          if (this.goods[i].select) {
            prize = prize + (this.goods[i].count * this.goods[i].prize)
          }

        }
        return prize * 100
      }
    },
    data: function () {
      return {
        edit: false,
        totle_prize: 0,
        allchecked: true,
        goodslength: 0
      }
    },
    watch: {
      allchecked: function () {
        if (this.allchecked == true) {
          for (var i = 0; i < this.goods.length; i++) {
            this.goods[i].select = true
          }
        }
      },
    },
    mounted() {

      this.$nextTick(() => {
        this.goodslength = this.goods.length
        this.totle_prize = this.totleprize()
      })
    },
    updated: function () {
      this.totle_prize = this.totleprize()
    },
    computed: {
      goods() {
        return store.state.cart
      }
    }
  }
</script>

<style lang="less">
  .van-nav-bar__text {
    color: @_green;
  }
</style>
<style lang="less" scoped>
  @width: 20px;
  @height: 165px;
  div {
    box-sizing: border-box;
  }

  .ctrl {
    position: absolute;
    height: @width;
    width: @width*3;
    right: 10px;
    bottom: 10px;
  }
  .delete{

  }
  .add {
    position: absolute;
    width: @width;
    height: @width;
    right: 0px;
    color: @_green;
    font-size: 20px;
    border-radius: 50%;
    border: 1px solid @_green;
    text-align: center;
  }

  .goodlist {
    height: calc(100vh - 220px);
    overflow: auto;
  }

  .plus {
    line-height: @width;
    right: 0px;
  }

  .reduce {
    line-height: 15px;
    right: 40px;
  }

  .number {
    display: inline-block;
    position: absolute;
    /*border: 1px solid ;*/
    width: @width;
    height: @width;
    right: 20px;
    text-align: center;
  }

  .contact {
    height: auto;
    background-color: #ffffff;
    margin-top: 10px;
  }

  .van-submit-bar {
    bottom: 50px;
  }

  .van-checkbox {
    margin-left: 15px;
  }

  .title {
    height: auto;
    overflow: hidden;
    line-height: 32px;
    border-bottom: 1px solid @_lgrey;
    padding: 0 10px;
  }

  .widge {
    float: right;
  }

  .good {
    height: @height;
    border-bottom: 1px solid @_lgrey;
    padding: 15px 0 15px 0;

  }

  .click {
    height: 100%;
    width: 10%;
    line-height: (@height)-30px;
    float: left;
    text-align: center;
  }

  .info {
    width: 90%;
    float: left;
  }

  .img {
    width: 30%;
    margin: 0 2.5%;
    height: (@height)-30px;
    line-height: (@height)-30px;;
    float: left;
    border: 1px solid #0d0d0d;
  }

  .img img {
    width: 100%;
  }

  .details {
    width: 65%;
    position: relative;
    float: left;
    height: (@height)-30px;
  }

  .name {
    color: @_blk;
    line-height: 30px;
  }

  .subname {
    line-height: 30px;
  }

  .prize {
    margin-top: 40px;
  }
</style>
